// file name: linearlist.hpp
// description: a linear list class, a data structure like arroy
// last editor: sunist chan
// macOS Catalina 10.15.5 with Xcode

#include <iostream>

class LinearList
{
private:
    int index;
    int size;
    int *start;
    
    void swap(int *a, int *b)
    {
        int tmp = *a;
        *a = *b;
        *b = tmp;
    }
    
public:

    LinearList(int size)
    {
        this -> size = size;
        start = (int*)malloc(size * sizeof(int));
    }

    LinearList(int size, int value)
    {
        this -> size = size;
        start = (int*)malloc(size * sizeof(int));
        for (int i = 0; i < size; i++)
        {
            *(start + sizeof(int) * i) = value;
        }
    }

    ~LinearList()
    {
        free(start);
    }

    int at(int index)
    {
        return *(start + index * sizeof(int));
    }

    int length()
    {
        return size;
    }
    
    void change(int index, int data)
    {
        *(start + index * sizeof(int)) = data;
    }
    
    void sort()
    {
        int bit = sizeof(int);
        int tmp = size;
        bool Sorted = false;
        while (!Sorted)
        {
            Sorted = true;
            for (int i = 1; i < tmp; i++)
            {
                if (*(start+i*bit) > *(start+i*bit-bit))
                {
                    swap(start+i*bit, start+i*bit-bit);
                    Sorted = false;
                }
            }
            tmp--;
        }
    }
    
    void sort(int begin, int end)
    {
        int bit = sizeof(int);
        int tmp = end;
        bool Sorted = false;
        while (!Sorted)
        {
            Sorted = true;
            for (int i = 1+begin; i < tmp; i++)
            {
                if (*(start+i*bit) > *(start+i*bit-bit))
                {
                    swap(start+i*bit, start+i*bit-bit);
                    Sorted = false;
                }
            }
            tmp--;
        }
    }
    
    void reverse()
    {
        int bit = sizeof(int);
        for (int i = 0; i < size/2; i++)
        {
            swap((start + i*bit), (start + size*bit - i*bit - bit));
        }
    }
    
    int max()
    {
        int maxval = -2147483648;
        for (int i = 0; i < size; i++)
        {
            if (at(i) > maxval)
            {
                maxval = at(i);
            }
        }
        return maxval;
    }
    
    int min()
    {
        int minval = 2147483647;
        for (int i = 0; i < size; i++)
        {
            if (at(i) < minval)
            {
                minval = at(i);
            }
        }
        return minval;
    }
};
